#!/usr/bin/perl
#
# Reprocess messages that are already stored in DB
#
use DBI;
use Mach;
use Data::Dumper;
use Getopt::Long;

$machinekey = 0;
$DEBUG = 0;
$version = '';

GetOptions(
	   "unparsedcpu" => \$unknowncpu,
	   "key=i" => \$machinekey,
	   "version=s" => \$version,
	   "debug" => \$DEBUG,
	   );

$Mach::DEBUG = $DEBUG;

$limit = shift;
$limit = 10 if !$limit;

if ($unknowncpu) {
    $stm = $Mach::dbh->prepare("SELECT lastmessage FROM automach
          WHERE cpu like 'UNPARSED%' or cpu is null limit $limit");
} elsif ($machinekey) {
    $stm = $Mach::dbh->prepare("SELECT lastmessage FROM automach
          WHERE f_key = $machinekey");
} elsif ($version) {
    warn "Selecting version $version\n";
    $stm = $Mach::dbh->prepare("SELECT lastmessage FROM automach
          WHERE method = 'machine-update version $version'");

} else {
    $stm = $Mach::dbh->prepare("SELECT lastmessage FROM automach limit $limit");
}
$stm->execute() || die "Select failed\n";
print "Got ", $stm->rows(), " records\n";
while ($t = $stm->fetchrow_arrayref()) {
  Mach::processmessage($$t[0]);
}
#print Dumper(\%Mach::mapnotes);
